﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LCUV;
using System.Data;
using Telerik.Web.UI;
using Oracle.DataAccess.Client;
using System.Web.UI.HtmlControls;

namespace sWarranty.WEB
{
    public partial class SOverify : System.Web.UI.Page
    {
        public string _currentUserEmpNo = "";
        public string _currentUserAuthority = "";
        public string _currentUserRegion = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            _currentUserEmpNo = GetUserLoginEmpNo();
            Session["UrltoFw"] = "SOverify.aspx";
            if (!IsPostBack)
            {
                txtToDate.Text = DateTime.Now.ToString("yyy/MM/dd");
                txtFromDate.Text = DateTime.Now.ToString("yyy/MM/dd");
            }
        }
        protected string GetUserLoginEmpNo()
        {
            string t = "";
            try
            {
                if (Session["UserLoginInfo"] != null)
                {
                    Entities db = new Entities();
                    var u = (TB_USER[])Session["UserLoginInfo"];
                    int _UID = Convert.ToInt32(u.Single().ID);
                    var au = from ft in db.TB_USER_LEVEL
                             where ft.USER_ID == _UID
                             select ft;
                    int _le = Convert.ToInt32(au.Single().LEVEL_ID);
                    var le = from xx in db.TB_LEVEL
                             where xx.ID == _le
                             select xx;
                    _currentUserAuthority = le.Single().REMARK;
                    _currentUserRegion = u.Single().REGION_ID;
                    hiddenUserRegion.Value = _currentUserRegion;
                    hiddenCurUser.Value = u.Single().EMPNO;
                    if (_currentUserRegion != "00")
                    {
                        DBAction.SetSelected(dropRegion, (u.Single().REGION_ID));
                        dropRegion.Enabled = false;
                    }
                    return u.Single().EMPNO;
                }
            }
            catch (Exception)
            {
                t = "";
            }
            return t;
        }
        protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
            string p_f_dt = Convert.ToDateTime(txtFromDate.Text).ToString("yyyMMdd");
            string p_t_dt = Convert.ToDateTime(txtToDate.Text).ToString("yyyMMdd");
            string p_shop_code = null;
            string p_shop_name = null;
            string p_vrf_flag = "%";
            string p_cfm_flag = "N";
            string p_user_region = _currentUserRegion;
            string p_region_id = "%";

            if (!string.IsNullOrEmpty(txtShopName.Text.Trim()))
                p_shop_name = txtShopName.Text.Trim();
            if (!string.IsNullOrEmpty(txtShopCode.Text.Trim()))
                p_shop_code = txtShopCode.Text.Trim();
            p_vrf_flag = dropVerified.SelectedValue;
            p_cfm_flag = dropConfirm.SelectedValue;
            if (dropRegion.SelectedValue != "00")
                p_region_id = dropRegion.SelectedValue;

            try
            {
                DataTable dt = new DataTable();
                dt = DBAction.runStore("PK_SOM_MST.PR_SO_DEALER_INQ", "uiroi", "p_shop_code", p_shop_code,
                    "p_shop_name", p_shop_name, "p_f_dt", p_f_dt, "p_t_dt", p_t_dt, "p_vrf_flag", p_vrf_flag,
                    "p_cfm_flag", p_cfm_flag, "p_user_region", p_user_region, "p_region_id", p_region_id,
                    null, null, null, null, null, null, null, null, null, null, null, null, null, null);
                try
                {
                    if (dt.Rows.Count > 0 && !string.IsNullOrEmpty(dt.Rows[0]["Error"].ToString()))
                    {
                        MessageBox.Show(dt.Rows[0]["Error"].ToString());
                    }
                }
                catch (Exception)
                {
                    //Column 'Error' does not belong to table <-- ko co' loi j va co' data , thi ko lam j
                }
                RadGrid1.DataSource = dt;
            }
            catch (Exception ee)
            {
                if (ee.InnerException != null)
                    MessageBox.Show(ee.InnerException.Message);
                else
                    MessageBox.Show(ee.Message);
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string p_f_dt = Convert.ToDateTime(txtFromDate.Text).ToString("yyyMMdd");
            string p_t_dt = Convert.ToDateTime(txtToDate.Text).ToString("yyyMMdd");
            string p_shop_code = null;
            string p_shop_name = null;
            string p_vrf_flag = "%";
            string p_cfm_flag = "N";
            string p_user_region = _currentUserRegion;
            string p_region_id = "%";

            if (!string.IsNullOrEmpty(txtShopName.Text.Trim()))
                p_shop_name = txtShopName.Text.Trim();
            if (!string.IsNullOrEmpty(txtShopCode.Text.Trim()))
                p_shop_code = txtShopCode.Text.Trim();
            p_vrf_flag = dropVerified.SelectedValue;
            p_cfm_flag = dropConfirm.SelectedValue;
            if (dropRegion.SelectedValue != "00")
                p_region_id = dropRegion.SelectedValue;


            try
            {
                DataTable dt = new DataTable();
                dt = DBAction.runStore("PK_SOM_MST.PR_SO_DEALER_INQ", "uiroi", "p_shop_code", p_shop_code,
                    "p_shop_name", p_shop_name, "p_f_dt", p_f_dt, "p_t_dt", p_t_dt, "p_vrf_flag", p_vrf_flag,
                    "p_cfm_flag", p_cfm_flag, "p_user_region", p_user_region, "p_region_id", p_region_id,
                    null, null, null, null, null, null, null, null, null, null, null, null, null, null);
                try
                {
                    if (dt.Rows.Count > 0 && !string.IsNullOrEmpty(dt.Rows[0]["Error"].ToString()))
                    {
                        MessageBox.Show(dt.Rows[0]["Error"].ToString());
                    }
                }
                catch (Exception)
                {
                    //Column 'Error' does not belong to table <-- ko co' loi j va co' data , thi ko lam j
                }
                RadGrid1.DataSource = dt;
                RadGrid1.DataBind();
            }
            catch (Exception ee)
            {
                if (ee.InnerException != null)
                    MessageBox.Show(ee.InnerException.Message);
                else
                    MessageBox.Show(ee.Message);
            }

        }

        protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
        {
            if (e.CommandName == "uiroi")
            {
                try
                {
                    GridDataItem item = e.Item as GridDataItem;
                    string p_shop_code = item["SHOP_CODE"].Text;
                    string p_shop_cell = item["SHOP_CELL"].Text;
                    string p_edi_no = item["EDI_NO"].Text;
                    p_edi_no = p_edi_no.Replace("<nobr>", "").Replace("</nobr>", "");
                    string p_serial_no = item["SERIAL_NO"].Text;
                    string p_model = item["MODEL"].Text;
                    string p_suffix = item["SUFFIX"].Text;
                    string p_sellout_date = item["SELLOUT_DATE"].Text;
                    string p_sellin_date = item["SELLIN_DATE"].Text;
                    string p_user_cell = item["user_cell"].Text;
                    string p_cfm_comment = "";
                    string p_cfm_user = _currentUserEmpNo;

                    string[] st = hid1.Value.Split('|');
                    //[0] = txtEDI_No
                    //[1] = Value user input
                    if (st[0].Replace("txt", "") == p_edi_no)
                    {
                        p_cfm_comment = st[1].ToString();

                        string error = "";
                        string stx = "select pk_som_mst.FN_SO_INCT_DEALER_SET_MNL_CFM('"
                            + p_shop_code + "','"
                            + p_shop_cell + "','"
                            + p_edi_no + "','"
                            + p_serial_no + "','"
                            + p_model + "','"
                            + p_suffix + "','"
                            + p_sellout_date + "','"
                            + p_sellin_date + "','"
                            + p_user_cell + "','"
                            + p_cfm_comment + "','"
                            + p_cfm_user + "') from dual";
                        OracleCommand cmd = DBAction.con.CreateCommand();
                        OracleParameter param = new OracleParameter();
                        cmd.CommandText = stx;
                        cmd.CommandType = CommandType.Text;
                        var x = cmd.ExecuteScalar();
                        error = x.ToString();
                        MessageBox.Show(error);
                    }
                }
                catch (Exception ee)
                {
                    MessageBox.Show(ee.Message);
                }
            }
        }
        protected void RadGrid1_ColumnCreated(object sender, GridColumnCreatedEventArgs e)
        {
            if (e.Column is GridBoundColumn)
            {
                (e.Column as GridBoundColumn).DataFormatString = "<nobr>{0}</nobr>"; //auto width
            }
        }

        protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
        {
            try
            {
                string p_serial_no = e.Item.Cells[6].Text;
                string p_cfm_flag = e.Item.Cells[13].Text;
                p_serial_no = p_serial_no.Replace("&nbsp;", "");
                p_cfm_flag = p_cfm_flag.Replace("<nobr>", "").Replace("</nobr>","");

                if (e.Item is GridDataItem)
                {
                    if (string.IsNullOrEmpty(p_serial_no) || p_cfm_flag == "Y")
                    {
                        GridDataItem item1 = (GridDataItem)e.Item;
                        Button btnEdit = (Button)item1.FindControl("btnEdit");
                        btnEdit.Enabled = false;
                    }
                }
            }
            catch (Exception ee)
            {
            }
        }

        protected void btnExcel_Click(object sender, EventArgs e)
        {

        }
    }
}